package com.hly.musicbackground.model.user.rest;

import com.hly.musicbackground.Utils.DozerUtils;
import com.hly.musicbackground.Utils.RequestContextUtils;
import com.hly.musicbackground.model.music.params.IDParams;
import com.hly.musicbackground.model.user.VO.UserInfoVO;
import com.hly.musicbackground.model.user.params.MailParams;
import com.hly.musicbackground.model.user.params.UserUpdateInfoParams;
import com.hly.musicbackground.model.user.service.UserLogService;
import com.hly.musicbackground.model.user.service.UserService;
import com.hly.musicbackground.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * 用户
 * */
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "用户")
public class UserController {

    private final UserLogService userLogService;
    private final UserService userService;

    private final DozerUtils dozerUtils;


    /**
     * 获取用户头像
     * @param mailParams
     * */
    @GetMapping("/headImage")
    @ApiOperation(value = "获取用户头像")
    public Result<String> headImage(MailParams mailParams){
        return Result.ok(userService.getHeadImage(mailParams.getMail()));
    }

    /**
     * 获取默认头像列表
     * */
    @GetMapping("/headImageDefault")
    @ApiOperation(value = "获取默认可选头像链接列表")
    public Result<ArrayList<String>> headImageDefault(){
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0;i < 21;i++){
            list.add("/images/headUserImage/default/" + i + ".png");
        }
        return Result.ok(list);
    }

    /**
     * 获取用户信息
     * */
    @GetMapping("/info")
    @ApiOperation(value = "获取用户信息")
    public Result<UserInfoVO> getInfo(IDParams params){
//        if (params.getId() == 0) {
//            params.setId(RequestContextUtils.getUser().getUserId());
//        }
        UserInfoVO userInfo = userService.getUserInfo(params.getId());
        return Result.ok(userInfo);
    }

    /**
     * 修改用户信息
     * */
    @PutMapping("/info")
    @ApiOperation(value = "修改用户信息")
    public Result updateInfo(@RequestBody UserUpdateInfoParams params){
        int userId = RequestContextUtils.getUser().getUserId();
        userService.updateInfo(userId, params);
        return Result.ok();
    }
}
